{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import scipy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import scipy.integrate"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from scipy.integrate import quad, dblquad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def I(n):\n",
    "    return dblquad(lambda t, x: np.exp(-x*t)/t**n, 0, np.inf, 1, np.inf)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0.33333333325010883, 1.3888461883425516e-08)\n"
     ]
    }
   ],
   "source": [
    "print(I(3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "gaussian = lambda x: 1/np.sqrt(np.pi) * np.exp(-x**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Romberg integration of <function vectorize1.<locals>.vfunc at 0x7f0dbd7a8378> from [0, 1]\n",
      "\n",
      " Steps  StepSize   Results\n",
      "     1  1.000000  0.385872 \n",
      "     2  0.500000  0.412631  0.421551 \n",
      "     4  0.250000  0.419184  0.421368  0.421356 \n",
      "     8  0.125000  0.420810  0.421352  0.421350  0.421350 \n",
      "    16  0.062500  0.421215  0.421350  0.421350  0.421350  0.421350 \n",
      "    32  0.031250  0.421317  0.421350  0.421350  0.421350  0.421350  0.421350 \n",
      "\n",
      "The final result is 0.421350396474754 after 33 function evaluations.\n"
     ]
    }
   ],
   "source": [
    "result = scipy.integrate.romberg(gaussian, 0, 1, show=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.421350396474754"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "result"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
